﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharedLib.BasicServices.MessageFramework;
using Tracy.Messages;
using System.Xml;
using System.Net;
using SharedLib.Helpers;

namespace Tracy.Handlers
{
    public class GetXmlFromURLHandler : TaskMessageHandler<GetXmlFromURLMessage>
    {

        protected override void ProcessMessage(GetXmlFromURLMessage msg)
        {
            string url = msg.GenericData;
            WebClient client = new WebClient();
            byte[] contentData = client.DownloadData(url);
            string content = StringHelper.SanitizeXmlString(Encoding.UTF8.GetString(contentData));
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.LoadXml(content);
                msg.Result = doc;
            }
            catch (Exception e)
            {
                msg.Exception = e;
            }
        }
    }
}
